package com.sun.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity
{

	private LinearLayout root;
	private RelativeLayout relativeLayout;
	private ImageView expand_status;

	private boolean isexpand = true;

	private ExpandableListView expandableListView;
	private ExpandableListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initUI();
	}

	// 初始化UI
	private void initUI()
	{
		initExpandableListView();
		// Layout
		root = (LinearLayout)findViewById(R.id.root);
		relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
		relativeLayout.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				if(isexpand) // 收缩
				{
					isexpand = false;
					root.removeView(expandableListView);
				}
				else
				// 展开
				{
					isexpand = true;
					RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
					rl.leftMargin = 30;
					root.addView(expandableListView,rl);
				}
				updateImageStatus(); // 更新列表展开与收缩的图标
			}
		});

		// 下面是启动初始化
		expand_status = (ImageView)findViewById(R.id.expand_status);
		updateImageStatus();

		if(isexpand)
		{
			RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
			rl.leftMargin = 30;
			root.addView(expandableListView,rl);
		}
		else
		{
			root.removeView(expandableListView);
		}
	}

	// 根据列表展开情况设置图标
	private void updateImageStatus()
	{
		if(isexpand)
		{
			expand_status.setImageResource(R.drawable.expand);
		}
		else
		{
			expand_status.setImageResource(R.drawable.close);
		}
	}

	// 初始化 ExpandableListView
	private void initExpandableListView()
	{
		expandableListView = new ExpandableListView(this);
		mAdapter = new MyExpandableListAdapter();
		expandableListView.setAdapter(mAdapter);
	}

	public class MyExpandableListAdapter extends BaseExpandableListAdapter
	{
		// Sample data set. children[i] contains the children (String[]) for
		// groups[i].
		private String[] groups = {"People Names","Dog Names","Cat Names","Fish Names"};
		private String[][] children = {{"Arnold","Barry","Chuck","David"},{"Ace","Bandit","Cha-Cha","Deuce"},{"Fluffy","Snuggles"},{"Goldy","Bubbles"}};

		public Object getChild(int groupPosition, int childPosition)
		{
			return children[groupPosition][childPosition];
		}

		public long getChildId(int groupPosition, int childPosition)
		{
			return childPosition;
		}

		public int getChildrenCount(int groupPosition)
		{
			return children[groupPosition].length;
		}

		public TextView getGenericView()
		{
			// Layout parameters for the ExpandableListView
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);

			TextView textView = new TextView(MainActivity.this);
			textView.setLayoutParams(lp);
			// Center the text vertically
			textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			// Set the text starting position
			textView.setPadding(36,0,0,0);
			return textView;
		}

		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
		{
			TextView textView = getGenericView();
			textView.setText(getChild(groupPosition,childPosition).toString());
			return textView;
		}

		public Object getGroup(int groupPosition)
		{
			return groups[groupPosition];
		}

		public int getGroupCount()
		{
			return groups.length;
		}

		public long getGroupId(int groupPosition)
		{
			return groupPosition;
		}

		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
		{
			TextView textView = getGenericView();
			textView.setText(getGroup(groupPosition).toString());
			return textView;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition)
		{
			return true;
		}

		public boolean hasStableIds()
		{
			return true;
		}

	}

}